﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Dto;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.financeBuyAdvanceMoney
{
    public partial class Edit : EditPagebase<IBuyAdvanceMoneyContract, BuyAdvanceMoney>
    {
        public ISupplierContract SupplierContract { get; set; }
        public IPayTypeContract PayTypeContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIListHelper.BindDDL(typeof(BuyPayBillTypeEnum).ToDescription(), ListItemHelper.EnumToListItems(typeof(BuyPayBillTypeEnum)), 0L, this.ed_PayBillType, true);
                //FineUIListHelper.BindDDL<Supplier>(SupplierContract.GetResolvedTree(), 0L, ed_Supplier,true);
                //FineUIListHelper.BindDDL(SupplierContract.EntityName, this.SupplierContract.GetSelectList(), 0L, ed_Supplier,true);
                //FineUIListHelper.BindDDL<PayType>(PayTypeContract.GetResolvedTree(), 0L, ed_PayType,true);
                FineUIListHelper.BindDDL(PayTypeContract.EntityName, this.PayTypeContract.GetSelectList(), 0L, ed_PayType, true);
                if (PageMode != pagemode.Add)
                    LoadData();
                else
                {
                    ed_UsedMoney.SetValue(0);
                    ed_PayDate.SetValue(System.DateTime.Now.TimeToStringDate());

                }
            }
        }

        protected override void PagePowerCheck()
        {
            base.PagePowerCheck();
            if (PageMode == pagemode.Update && this.EnableEdit)
            {
                var PowerEntity = Manager.GetById(EntityId);
                this.EnableEdit = this.EnableEdit && PowerEntity.PassState == CanDoo.Core.Data.PassState.Wait && !PowerEntity.IsDeleted;
            }
        }

        protected override void LoadData(BuyAdvanceMoney entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_Code.SetValue(entity.Code);
            ed_PayDate.SetValue(entity.PayDate);
            if (entity.Supplier != null)
            {
                ed_Supplier.Values = new string[] { entity.Supplier.Id.ToString() };
                ed_Supplier.Text = entity.Supplier.Name;
            }
            ed_PayBillType.SetValue((int)entity.PayBillType);
            if (!entity.PayTypeId.IsNull())
                ed_PayType.SetValue(entity.PayTypeId);
            ed_Money.SetValue(entity.Money);
            ed_UsedMoney.SetValue(entity.UsedMoney);
            ed_Remark.SetValue(entity.Remark);
        }

        protected override OperationResult SaveData(BuyAdvanceMoney entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            if (PageMode == pagemode.Add)
            {
                entity.Code = Manager.GetNewCode();
            }
            entity.PayDate = ed_PayDate.GetValue().ToNullableDateTime();
            if (entity.PayDate == DateTime.MinValue)
                entity.PayDate = null;
            if (ed_Supplier.Values.Count() > 0 && ed_Supplier.Values[0].ToLong() >  0L)
                entity.SupplierId = ed_Supplier.Values[0].ToLong();
            else
                entity.SupplierId = 0L;
            entity.PayBillType = ed_PayBillType.GetValue().To<BuyPayBillTypeEnum>();
            if (ed_PayType.GetValue().ToLong() == 0L)
                entity.PayTypeId = 0L;
            else
                entity.PayTypeId = ed_PayType.GetValue().ToLong();
            entity.Money = ed_Money.GetValue().ToDouble();
            if (PageMode == pagemode.Add)
                entity.UsedMoney = 0;
            entity.Remark = ed_Remark.GetValue().ToString();
            return base.SaveData(entity);

        }
    }
}
